//
//  HomeViewController.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewAccessibilityDelegate, NewsDelegate, FashionDelegate, AmuseyuleDelegate, HistoryDelegate, ScienceDelegate>
@property (nonatomic, retain)UICollectionView *collectionView;
// 头部滑动
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *buttonArr;
@end

@implementation HomeViewController
- (void)dealloc
{
    [_collectionView release];
    [_scrollView release];
    [_tableView release];
    [_buttonArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 设置透明
    self.navigationController.navigationBar.translucent = NO;
    
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    // 导航栏上按钮颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    // 24小时要闻
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"24"] style:UIBarButtonItemStylePlain target:self action:@selector(twofourAction:)];
    // 搜索
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:@selector(searchAction:)];
    
    self.navigationItem.title = @"网易新闻";

    // 创建collectionView
    [self createCollectionView];
    // 创建头部滚动视图
    [self createScroll];
    
    [NSTimer scheduledTimerWithTimeInterval:3600 target:self selector:@selector(notClick:) userInfo:nil repeats:YES];
}
- (void)notClick:(id)sender
{
    [HomeViewController registerLacalNotification:4];// 4秒后
}
// 设置本地通知
+ (void)registerLacalNotification:(NSInteger)alertTime
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    // 设置触发通知的时间
    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
    NSLog(@"fireDate = %@", fireDate);
    notification.fireDate = fireDate;
    // 时区
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 设置重复的间隔
    notification.repeatInterval = kCFCalendarUnitSecond;
    // 通知内容
    notification.alertBody = @"精彩内容";
    notification.applicationIconBadgeNumber = 1;
    // 通知被触发时播放的声音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 通知参数
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"......" forKey:@"key"];
    notification.userInfo = userDict;
    // ios8后,需要添加这个注册,才能得到授权
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        // 通知重复提示的单位,可以是天,周,月
        notification.repeatInterval = NSCalendarUnitDay;
    }else{
        // 通知重复提示的单位,可以是天,周,月
        notification.repeatInterval = NSDayCalendarUnit;
    }
    // 执行通知注册
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
// 取消某个本地推送通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key
{
    // 获取所有本地通知数组
    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    for (UILocalNotification *notification in localNotifications) {
        NSDictionary *userInfo = notification.userInfo;
        if (userInfo) {
            // 根据设置通知参数时指定的key来获取通知参数
            NSString *info = userInfo[key];
            // 如果找到需要取消的通知 则取消
            if (info != nil) {
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
                break;
            }
        }
    }
}
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH * 375,self.view.frame.size.height - HEIGHT * 55);
    // 滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 边界预留
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    // 列间距
    layout.minimumLineSpacing = 0;
    // 创建
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    // 允许滚动
    _collectionView.pagingEnabled = YES;
    // 注册
    [_collectionView registerClass:[NewsCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerClass:[FashionCell class] forCellWithReuseIdentifier:@"celltwo"];
    [_collectionView registerClass:[AmuseCell class] forCellWithReuseIdentifier:@"cellthree"];
    [_collectionView registerClass:[HistoryCell class] forCellWithReuseIdentifier:@"cellfour"];
//    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellfive"];
    [_collectionView registerClass:[ScienceCell class] forCellWithReuseIdentifier:@"cellsix"];
    
    [self.view addSubview:_collectionView];
    [_collectionView release];

}
// 个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}
// 样式  collectionView套tableView
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        NewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.row == 1){
        FashionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"celltwo" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.row == 2){
        AmuseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellthree" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;

    }else if (indexPath.row == 3){
        HistoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellfour" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;

    }else{
        ScienceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellsix" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
}
- (void)createScroll
{
    NSArray *arr = @[@"头条", @"时尚", @"娱乐", @"历史", @"科技"];
    self.buttonArr = [NSMutableArray array];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 40)];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(WIDTH * 70 * arr.count, 0);
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    // 添加按钮
    for (NSInteger i = 0; i < arr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(WIDTH * 70 * i, 0, WIDTH * 70, HEIGHT * 40);
        button.tag = 100 + i;
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArr addObject:button];
        [_scrollView addSubview:button];
    }
    // 首次进入页面 第一个按钮为红色
    [_buttonArr[0] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_scrollView release];
}
- (void)buttonClick:(UIButton *)button
{
    // 遍历button 所有button为黑色
    for (UIButton *button in _buttonArr) {
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    // 选中按钮为红色
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
     [_collectionView setContentOffset:CGPointMake(WIDTH * 375 * (button.tag - 100), 0) animated:YES];

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / self.view.frame.size.width;
    
    for (UIButton *button in _buttonArr) {
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    // 对应的页面的按钮为红色
    [_buttonArr[i] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

}
- (void)newsValue:(NSString *)url value1:(NSString *)ti{
    NewSelectedVC *new = [[NewSelectedVC alloc] init];
    new.str = url;
    [self.navigationController pushViewController:new animated:YES];
    [new release];
}
- (void)fashionValue:(NSString *)url
{
    FashionSelectVC *fash = [[FashionSelectVC alloc] init];
    fash.str = url;
    [self.navigationController pushViewController:fash animated:YES];
    [fash release];
}
- (void)amuseyuleDelegate:(NSString *)url
{
    AmuseSelectVC *amu = [[AmuseSelectVC alloc] init];
    amu.str = url;
    [self.navigationController pushViewController:amu animated:YES];
    [amu release];

}
- (void)historyValue:(NSString *)url
{
    HistorySelectVC *his = [[HistorySelectVC alloc] init];
    his.str = url;
    [self.navigationController pushViewController:his animated:YES];
    [his release];
}
- (void)scienceValue:(NSString *)url
{
    ScienceSelectVC *sci = [[ScienceSelectVC alloc] init];
    sci.str = url;
    [self.navigationController pushViewController:sci animated:YES];
    [sci release];
}
// 24小时要闻
- (void)twofourAction:(UIBarButtonItem *)twofour
{
    ImportantNewsVC *import = [[ImportantNewsVC alloc] init];
    [self.navigationController pushViewController:import animated:YES];
    [import release];
}
// 搜索
- (void)searchAction:(UIBarButtonItem *)search
{
    SearchVC *seaVC = [[SearchVC alloc] init];
    UINavigationController *uina = [[UINavigationController alloc]initWithRootViewController:seaVC];
    [self presentViewController:uina animated:YES completion:^{
        NSLog(@"模态");
    }];
//    [self.navigationController pushViewController:seaVC animated:YES];
    [seaVC release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
